串口调试助手工具的源代码
private void SendButton_MouseUp(object sender, MouseEventArgs e)
{
SendButton.Image = 串口调试.Properties.Resources.Send_MouseEnter;
if (!COM.IsOpen)
{
ShowSysMessage(Color.Red, "请打开一个端口!");
return;
}
if (SendtextBox.TextLength == 0)
{
ShowSysMessage(Color.Red, "请输入数据!");
}
else
{
if (HexSendChecked) //16进制发送
{
if (SendtextBox.Text.Length > 2) //大于两个字符
{
ShowSysMessage(Color.Red, "格式错误,请输入一个十六进制数!");
SendtextBox.Clear();
}
else if (SendtextBox.Text.Length == 2) //两个字符
{
char byteH = SendtextBox.Text[0];
char byteL = SendtextBox.Text[1];
if (ascIItoHex(ref byteH) && ascIItoHex(ref byteL))
{
byte[] b = new byte[1];
b[0] = (byte)((byteH << 4) | byteL);
COM.Write(b, 0, 1);
ShowSysMessage(Color.YellowGreen, "0x" SendtextBox.Text);
SendtextBox.Clear();
}
else //输入不为0~9 A~F a~f
{
ShowSysMessage(Color.Red, "格式错误,请输入一个十六进制数!");
SendtextBox.Clear();
}
}
else //一个字符
{
char c = SendtextBox.Text[0];
if (ascIItoHex(ref c))
{
byte[] b = new byte[1];
b[0] = (byte)c;
COM.Write(b, 0, 1);
ShowSysMessage(Color.YellowGreen, "0x0" SendtextBox.Text);
SendtextBox.Clear();
}
else
{
ShowSysMessage(Color.Red, "格式错误,请输入一个十六进制数!");
SendtextBox.Clear();
}
}
}
else //ASCII 发送
{
COM.Write(SendtextBox.Text);
ShowSysMessage(Color.YellowGreen, SendtextBox.Text);
SendtextBox.Clear();
}
}
} bool HexShowChecked = false;
private void HexCheckpictureBox_MouseEnter(object sender, EventArgs e)
{
if (HexShowChecked)
{
HexCheckpictureBox.Image = 串口调试.Properties.Resources.CheckboxCheckedEnter;
}
else
{
HexCheckpictureBox.Image = 串口调试.Properties.Resources.CheckboxNoCheckEnter;
}
}
评论